﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QLCN
{
    public partial class FrmSQL : Form

    {
        private string ServerName = Chung.ServerName;
        private string DatabaseName = Chung.DatabaseName;
        private string UserDatabase = Chung.UserDatabase;
        private string PassWordDatabase = Chung.PasswordDatabase;
        private string ChuoiKetNoi = "";
        private SqlDataReader myDataReader;
        private SqlDataAdapter mydataAdapter;
        private SqlConnection myConnection;
        private SqlCommand myCommand;
        private DataSet myDataSet;
        private DataTable myTable;
        private string sql;

        public FrmSQL()
        {
            InitializeComponent();
        }

        private void FrmSQL_Load(object sender, EventArgs e)
        {
            ChuoiKetNoi = "Data Source = " + ServerName + ";" +
                            "Initial Catalog = " + DatabaseName + ";" +
                            "persist security info = true;" +
                            "User Id=" + UserDatabase +
                            "; Password='" + PassWordDatabase +
                            "'; Connect Timeout =50";
        }
        private void Display(String sql)
        {
            //mo ket noi
            myConnection = new SqlConnection(ChuoiKetNoi);
            try
            {
                myConnection.Open();               
                // tao thong qua ket noi da mo
                mydataAdapter = new SqlDataAdapter(sql, myConnection);
                myDataSet = new DataSet();
                mydataAdapter.Fill(myDataSet, "Hoso");
                myTable = myDataSet.Tables["Hoso"];
                //chuyen len luoi
                dataGridView1.DataSource = myTable;
                                            
               
            }
            catch (Exception ex)
            {
                //MessageBox.Show("Đang có lỗi nạp lên lưới : " + ex.Message);
            }
        }

        private void Btnthucthi_Click(object sender, EventArgs e)
        {
              try
            {
            //mo ket noi
            myConnection = new SqlConnection(ChuoiKetNoi);
            myConnection.Open();

            sql = RtbSql.Text.Trim();

            SqlCommand myCommand = new SqlCommand(sql, myConnection);
            myCommand.ExecuteNonQuery();
            myCommand.Dispose(); //

           // MessageBox.Show();

            Display(sql);
            }
              catch (Exception ex)
              {
                  MessageBox.Show("Đang có lỗi truy van sql : " + ex.Message);
              }
        }
        private void Btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Btnxoa_Click(object sender, EventArgs e)
        {
            RtbSql.Text = "";
        }

       
        
    }
}
